import br.com.nooclix.salon.ui.dto.Employee.EmployeeTO;
import br.com.nooclix.salon.ui.dto.attendance.AttendanceSheetTO;
import br.com.nooclix.salon.ui.dto.attendance.AttendanceTO;
import br.com.nooclix.salon.ui.dto.attendance.ReportAttendanceTO;
import br.com.nooclix.salon.ui.util.ResponseData;
import br.com.nooclix.salon.ui.util.Utilitys;
import br.com.nooclix.salon.ui.util.WindowsMessage;

import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

private var repAttList:ArrayCollection;

protected var ipHost:String = null;

private function findReportEmployeeAndPeriodResult(event:ResultEvent):void{
	var responseData:ResponseData = event.result as ResponseData;
	if(responseData.status == ResponseData.ok()){
		gridReport.dataProvider = responseData.items;
		this.repAttList = responseData.items ;
	}else{
		WindowsMessage.alarm(responseData.message + "Erro");
	}
		inputLabel();
}
private function inputLabel():void{
	for each(var rep:ReportAttendanceTO in repAttList){
		//WindowsMessage.alarm(rep.totalComission + " Valor do TotalComission");
	    fieldTotalComission.text = Utilitys.formatCurrencyBR(rep.totalComission).toString();
		fieldTotalValue.text = Utilitys.formatCurrencyBR(rep.totalValue).toString();
	}
}

private function findEmployeeByNameResult(event:ResultEvent):void{
	var responseData:ResponseData = event.result as ResponseData;
	if(responseData.status == ResponseData.ok()){
		cbEmployee.dataProvider = responseData.items;
	}else{
		WindowsMessage.alarm(responseData.message);
		
	}
}

public function btnPrintClick():void{
	servletUtilRemote.getHostAddress();
}

public function getHostAddressResult(event:ResultEvent):void
{
	ipHost = event.result.toString();
	createComissionReport();	
}

private function createComissionReport():void{
	
	var codeE:String = (cbEmployee.selectedItem as EmployeeTO).code.toString();
	
	
	//Data inicial
	var dayStart:String = fieldDateStart.selectedDate.date.toString();
	var mounthStart:String = fieldDateStart.selectedDate.month.toString();
	var yearStart:String = fieldDateStart.selectedDate.fullYear.toString();
	
	
	//Data final
	var dayEnd:String = fieldDateEnd.selectedDate.date.toString();
	var mounthEnd:String = fieldDateEnd.selectedDate.month.toString();
	var yearEnd:String = fieldDateEnd.selectedDate.fullYear.toString();
	
	var url:URLRequest = new URLRequest("http://"+ipHost+":8080/salon-web/comissionreport.nooclix");
	
	//Definindo o metodo da requisicao
	url.method = URLRequestMethod.POST;
	
	//Definindo variaveis da URL
	var variables:URLVariables = new URLVariables();
	variables.code = codeE; //criando a variavel
	variables.dStart = dayStart;
	variables.mStart = mounthStart;
	variables.yStart = yearStart;
	
	variables.dEnd = dayEnd;
	variables.mEnd = mounthEnd;
	variables.yEnd = yearEnd;
	
	//Adicionando o objeto que contem as variaveis na requisicao
	url.data = variables;
	
	//Efetuar a requisicao
	navigateToURL(url, "_blank");
	
}


private function findReportEmployeeAndPeriod():void{
	if(fieldDateStart.selectedDate == null || fieldDateEnd.selectedDate == null){
		WindowsMessage.alarm("Informe o período inicial e final para efetuar a pesquisa!");
		return;
	}
	reportRemote.findReportEmployeeAndPeriod(cbEmployee.selectedItem, fieldDateStart.selectedDate, fieldDateEnd.selectedDate);
}
private function cbEmployeeLabelFunction(item:Object):String{
	var e:EmployeeTO = item as EmployeeTO;
	return e.person.name;
}
private function findEmployeeByName():void{
	employeeRemote.findByName(fieldEmployeeName.text);
}